package com.xxl.job.executor.service.jobhandler;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.entity.Student;
import com.xxl.job.executor.service.stu.IStudentService;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 任务Handler示例（Bean模式）
 *
 * 开发步骤： 1、继承"IJobHandler"：“com.xxl.job.core.handler.IJobHandler”； 2、注册到执行器工厂：在
 * "JFinalCoreConfig.initXxlJobExecutor"
 * 中手动注册，注解key值对应的是调度中心新建任务的JobHandler属性的值。 3、执行日志：需要通过 "XxlJobLogger.log"
 * 打印执行日志；
 *
 * @author xuxueli 2015-12-19 19:43:36
 */
@JobHandler(value = "taoJobHandler")
@Component
public class TaoJobHandler extends IJobHandler {
	static Log log = LogFactory.get();
	private static long times = 0;

	@Autowired
	private IStudentService iStudentService;

	@Override
	public ReturnT<String> execute(String param) throws Exception {
		System.out.println("param:" + param);
		log.info("XXL-JOB, Hello World. 参数:{}", param);
		log.info("begin ...xxl-job-executor-sample-springboot times:" + times++);
		for (int i = 0; i < 5; i++) {
			XxlJobLogger.log("beat at:" + i);
			long begin = System.currentTimeMillis();
			Student student = new Student();
			student.setStuName("taox");
			Page<Student> page = new Page<Student>(1, 10);
			EntityWrapper<Student> eWrapper = new EntityWrapper<Student>(student);
			Page<Student> list = iStudentService.selectPage(page, eWrapper);
			System.out.println(JSONUtil.toJsonStr(list));
			long end = System.currentTimeMillis();
			// String json = JsonUtils.object2Json(list);
			System.out.println("begin:" + begin + " end:" + end + " end-begin:" + (end - begin));
			TimeUnit.SECONDS.sleep(2);
		}
		System.out.println("end ....\n");
		return SUCCESS;
	}

}
